(function($) {
	$.fn.chart = function(ops) {
		var self = this;
		var o = $.extend({}, $.fn.chart.ops, ops);
		$.fn.chart.init(o);
		var paper = Raphael(self[0], o.sx * o.nx + o.l + o.r, o.sy * o.ny + o.t + o.th + o.b);
		var pathArr = ["M", o.l + 0.5, o.t + o.th + 0.5, "L", o.sx * o.nx + o.l + 0.5, o.t + o.th + 0.5, "L", o.sx * o.nx + o.l + 0.5, o.sy * o.ny + o.t + o.th + 0.5, "L", o.l + 0.5, o.sy * o.ny + o.t + o.th + 0.5, "L", o.l + 0.5, o.t + o.th + 0.5];
		for (var i = 1; i < o.ny; i++) {
			pathArr = pathArr.concat(["M", o.l + 0.5, o.sy * i + o.t + o.th + 0.5, "H", o.sx * o.nx + o.l + 0.5]);
		}
		for (var i = 1; i < o.nx; i++) {
			pathArr = pathArr.concat(["M", o.sx * i + o.l + 0.5, o.t + o.th + 0.5, "V", o.sy * o.ny + o.t + o.th + 0.5]);
		}
		paper.path(pathArr.join(","));
		for (var i = 0; i <= o.ny; i++) {
			paper.text(o.l / 2 + 0.5, o.sy * i + o.t + o.th + 0.5, (o.ny - i) * o.ky).attr({"font-size": "12px"});
		}
		var tmpAttr = {"font-size": "12px"};
		if ((o.data[0][0] + "").length > 2) tmpAttr["transform"] = "r-60";
		for (var i = 1; i < o.nx; i++) {
			paper.text(o.sx * i + o.l + 0.5, o.sy * o.ny + o.t + o.th + o.b / 2 + 0.5,  o.data[i - 1][0]).attr(tmpAttr);
		}
		paper.text((o.sx * o.nx + o.l) / 2 + 0.5, o.th / 2 + 0.5, o.tl).attr({"font-size": "13px", "font-weight": "bold"});
		paper.text(o.sx * (o.nx) + o.l + o.r / 2 + 0.5, o.sy * o.ny + o.t + o.th + 0.5, o.tx).attr({"font-size": "12px"});
		paper.text(o.l + 0.5, o.t / 2 + o.th + 0.5, o.ty).attr({"font-size": "12px"});
		
		var circleArr = [], linePathArr = [];
		for(var i = 1; i < o.nx; i++) {
			var circle = paper.circle(o.sx * i + o.l + 0.5, o.sy * (o.ny - o.data[i - 1][1] / o.ky) + o.t + o.th + 0.5, o.cr).attr({"fill": "#F00", "stroke": "#F00"});
			circleArr.push(circle);
			linePathArr = linePathArr.concat([i == 1 ? "M" : "L", o.sx * i + o.l + 0.5, o.sy * (o.ny - o.data[i - 1][1] / o.ky) + o.t + o.th + 0.5]);
		}
		paper.path(linePathArr.join(",")).attr({"stroke": "#F00", "stroke-width": o.lw});
		$.each(circleArr, function(i, circle) {
			circle.hover(
				function() {
					var cx = this.attr("cx"), cy = this.attr("cy"), txt = o.data[i][1] + "";
					var x = cx + 10, y = cy + 10, w = txt.length * 10 + 10, h = 20;
					var tmpPath = ["M", x, y, "L", x + w, y, "L", x + w, y + h, "L", x, y + h, "L", x, y];
					self.titleFrame = paper.path(tmpPath.join(",")).attr({"fill": "#000", "stroke": "#666", "stroke-width": 2, "fill-opacity": 0.9});
					self.titleText = paper.text(x + w / 2 + 0.5, y + h / 2 + 0.5, txt).attr({"fill": "#FFF", "font-size": "13px"});
				},
				function() {
					if (self.titleFrame) self.titleFrame.remove();
					if (self.titleText)	self.titleText.remove();
				}
			);
		});
		
		return paper;
	};
	
	$.fn.chart.ops = {
		t: 40, r: 40, b: 30, l: 30, th: 20,
		sx: 30, sy: 30, ny: 10, nx: 10,
		cr: 5, lw: 1,
		tx: "x", ty: "y", tl: "",
		data: {1: 56, 2: 12, 3: 78, 4: 35}
	};
	
	$.fn.chart.init = function(ops) {
		var tmpData = [];
		var maxV = 0;
		for (var k in ops.data) {
			tmpData.push([k, ops.data[k]]);
			if (maxV < ops.data[k]) maxV = ops.data[k];
		}
		ops.data = tmpData;
		ops.nx = tmpData.length + 1;
		ops.ky = Math.ceil(maxV / ops.ny);
		var tmpb = (ops.data[0][0] + "").length * 6;
		ops.b = (tmpb < 30 ? 30 : tmpb);
		if (!ops.tl || ops.tl == "") ops.th = 0;
	};
	
})(jQuery);